package com.qingzhuge.member.service.impl;

import com.qingzhuge.member.service.ISysMemberService;
import com.qingzhuge.sms.service.IAccountValidateHandler;
import com.qingzhuge.sys.entity.SysSiteConfig;
import com.qingzhuge.sys.service.ISysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : zero.xiao
 * @description : 验证会员的手机号是否存在
 * @date : 2019/1/8 0008 上午 11:25
 * @modified :
 * @since :1.0.2
 * 仅仅在当前上下文中不存在IAccountValidateHandler实例对象时，才会实例化一个Bean
 */
@Component
@ConditionalOnMissingBean(name = "accountSmsCodeValidateUserHandlerImpl")
public class AccountSmsCodeValidateMemberHandlerImpl implements IAccountValidateHandler {
    private final ISysMemberService memberService;
    @Resource
    private ISysConfigService configService;

    @Autowired
    public AccountSmsCodeValidateMemberHandlerImpl(ISysMemberService userService) {
        this.memberService = userService;
    }

    @Override
    public boolean validate(String phone) {
        SysSiteConfig config = configService.getSiteConfig();
        //开启会员登录时用户不存在就注册新用户
        if (!config.isMemberLoginNotRegister() && StringUtils.isNotBlank(phone)) {
            return null != memberService.findByMobile(phone);
        }
        return true;
    }
}
